/**
 * <p>浏览数据的form，有上一页，下一页</p>
 * xtype = cceafviewformpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.ViewFormPanel
 * @extends Ext.form.Panel
 * @since 1.0
 */
Ext.define('cceaf.control.ViewFormPanel',{
	extend: 'Ext.form.Panel', 
    alias: 'widget.cceafviewformpanel',
    requires: [
        'cceaf.control.FieldSet',
  	    'cceaf.control.Label',
  	    'cceaf.control.Button'
  	],
          	
	constructor: function(config){
		config = config || {};
        
		cceaf.control.ViewFormPanel.superclass.constructor.apply(this, arguments);
	},
	
	/**
	 * 操作按钮定义区
	 *
     * @property operateArea 
     * @type Object[]
     * @default null
     */
    operateArea: null,
    
    /**
	 * 操作按钮布局方式：<p>
	 * standard：默认标准方式，按钮在最底部居中布局<p>
	 * bottom:toolbar的底部布局，按钮左对其<p>
	 * top:toolbar的顶部布局，按钮右对其<p>
	 *
     * @property buttonLayout 
     * @type String
     * @default 'top'
     */
    buttonLayout: 'top',
    
    /**
	 * 是否有关闭按钮
	 *
     * @property hasClose 
     * @type Boolean
     * @default false
     */
    hasClose: false,
    
    /**
	 * 载入的数据
	 *
     * @property datas 
     * @type Object[]
     * @default null
     */
    datas: null,
    
    /**
	 * 总的页数
	 *
     * @property totalPageCount 
     * @type Number
     * @default 0
     */
    totalPageCount: 0,
    
    /**
	 * 当前页序号
	 *
     * @property currentPageNo 
     * @type Number
     * @default 0
     */
    currentPageNo: 0,
    
    /**
	 * 表示子表数据集合的字段名称
	 *
     * @property detailDataName 
     * @type Object
     * @default null
     */
    detailDataName: null,
    
	initComponent : function(){
		var config = this;
		
		var layout = config.layout;
		var columns = config.columns;
        if(columns != null){
        	layout = {
	        	type: 'table',
	        	columns: columns
	        };
        	config = Ext.apply(config, {layout: layout});
        } else {
        	defaults = {  
    			//bodyStyle:'padding:0px 5px 0px 0px',
    			layout: {
			        type: 'hbox',
			        align: 'middle'
			    },
    			baseCls:'x-plain',
    			xtype:'cceaffieldset'
            };
        }
		
		var operateArea = [],i=0;
		if (config.operateArea) {
			operateArea = config.operateArea;
		} else { //默认操作区
			if (config.hasClose) {
				operateArea[i] = {itemId: 'btnClose', text: '关闭', xtype: 'cceafbutton', iconCls: 'closeIcon'};
				i++;
			}
			{
				operateArea[i] = {xtype: 'tbfill'};
				i++;
				operateArea[i] = '-';
				i++;
			}
			{
				operateArea[i] = {label: 'cceaflabel', itemId: 'pageInfo'};
				i++;
				operateArea[i] = '-';
				i++;
			}
			{
				operateArea[i] = {xtype: 'cceaflabel', html: '          '};
				i++;
				operateArea[i] = '-';
				i++;
			}
			{
				operateArea[i] = {itemId: 'btnPrevious', text: '上一页', xtype: 'cceafbutton', iconCls: 'previousIcon'};
				i++;
				operateArea[i] = '-';
				i++;
			}
			{
				operateArea[i] = {itemId: 'btnNext', text: '下一页', xtype: 'cceafbutton', iconCls: 'nextIcon'};
				i++;
				operateArea[i] = '-';
				i++;
			}
		
		}
		
		var buttonLayout = config.buttonLayout;
		var buttons;
		var dockedItems;
		if (buttonLayout == 'standard') {
			buttons = operateArea;
		} else if (buttonLayout == 'bottom') {
			dockedItems = [{
	            xtype: 'toolbar',
	            dock: 'bottom',
	            border: true,
	            layout: { pack: 'end'},
	            items: operateArea
	        }];
		} else if (buttonLayout == 'top') {
			dockedItems = [{
	            xtype: 'toolbar',
	            dock: 'top',
	            border: true,
	            items: operateArea
	        }];
		}
		
		var store;
		if (config.dataAction && config.model) {
    		store = new Ext.data.Store({
    	    	proxy: {
    	    		type: 'ajax',
    	    		url:  config.dataAction,
    	    		actionMethods: {  
    		            read: 'POST'  
    		        },  
    	    		reader: {
    					type: 'json',
    					root: 'data',
    					totalProperty: 'totalCount',
    					messageProperty: 'message'
    				}
    			},
    			
    	        listeners: {
    	        	'load': function(){
    	        		var jr = store.getProxy().getReader();
    	        		if(!jr.jsonData) {
    	        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
    	        			return;
    	        		}
    	        		var message = jr.jsonData.message;
    	        		var url = jr.jsonData.url;
    	        		var datas = jr.jsonData.data;
    	        		var totalCount = jr.jsonData.totalCount;

    	        		//设置数据
    	        		var billForm = config;
    	        		billForm.totalPageCount = totalCount;
    	        		
    	        		//alert(billForm.totalPageCount);
    	        		
    	        		if (billForm.datas) {
    	        			billForm.datas = Ext.Array.union(billForm.datas, datas);
    	        		} else {
    	        			billForm.datas = datas;
    	        		}
    	        		
    	        		var pageInfo = billForm.down('#pageInfo');
    	            	var btnPrevious = billForm.down('#btnPrevious');
    	            	var btnNext = billForm.down('#btnNext');
    	        		
    	            	if(!billForm.currentPageNo) {
    	            		billForm.currentPageNo = 1;
    	            	}
    	        		
    	        		if (billForm.currentPageNo < billForm.totalPageCount) {
    	         			btnNext.setDisabled(false);
    	         		} else {
    	         			btnNext.setDisabled(true);
    	         		}
    	         		if (billForm.currentPageNo > 1) {
    	         			btnPrevious.setDisabled(false);
    	         		} else {
    	         			btnPrevious.setDisabled(true);
    	         		}
    	        		
    	        		billForm.form.setValues(billForm.datas[billForm.currentPageNo-1]);
    	        		pageInfo.setText(Ext.util.Format.format("第{0}页 /共 {1}页", billForm.currentPageNo, billForm.totalPageCount));
    	        		
    	        		cceaf.util.ExtHelper.showInfoDialog(message,url);
    	        	},
    	        	'loadexception': function(){
    	        		var jr = store.getProxy().getReader();
    	        		if(jr.jsonData){
    		        		var message = jr.jsonData.message;
    		        		var url = jr.jsonData.url;
    		        		cceaf.util.ExtHelper.showErrorDialog(message,url);
    	        		}
    	        		else{
    	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
    	        		}
    	        	}
    	        	
    	        },
    	        autoDestroy: true,  
    	    	pageSize: szpshang.Global.PAGE_SIZE,
    	    	model: config.model
    	    });
	    }
		
		Ext.applyIf(this, {
        	bodyStyle: 'padding:10px 10px',
        	waitMsgTarget: true,
	        autoScroll:true,
	        defaults: defaults,
	        buttons: buttons,
	        dockedItems: dockedItems,
	        store: store
		});
	    
		cceaf.control.ViewFormPanel.superclass.initComponent.call(this);
    }
	
});